// 修改密码页面的 JS 文件
Page({
    data: {
        oldPassword: '',
        newPassword: '',
        confirmNewPassword: '',
        user: {}
    },

    bindOldPasswordInput: function (e) {
        this.setData({
            oldPassword: e.detail.value
        });
    },
    bindNewPasswordInput: function (e) {
        this.setData({
            newPassword: e.detail.value
        });
    },
    bindConfirmNewPasswordInput: function (e) {
        this.setData({
            confirmNewPassword: e.detail.value
        });
    },
    onSubmit: function () {
        if (this.data.oldPassword == this.data.user.password) {
            if (this.data.newPassword !== this.data.confirmNewPassword) {
                wx.showToast({
                    title: '新密码和确认密码不一致',
                    icon: 'none'
                });
                return;
            }
           
            console.log( this.data.user)
            wx.request({
                url: 'http://localhost:9090/upmembers',
                data:{
                    memberId: this.data.user.memberId,
                    password:this.data.newPassword 
                },
                method:"PUT",
                success: (res) => {
                    console.log(res)
                   
                    wx.showToast({
                        title: '密码修改成功',
                        icon: 'success'
                    });
                }
            })

        } else {
            wx.showToast({
                title: '密码输入错误',
                icon: 'none'
            })
        }
    },
    // 检查新密码和确认密码是否一致

    // 这里添加发送修改密码请求的代码

    onCancel: function () {
        // 取消修改密码
        wx.showToast({
            title: '已取消修改',
            icon: 'none'
        });
        // 清空输入框
        this.setData({
            oldPassword: '',
            newPassword: '',
            confirmNewPassword: '',
        });
    },
    onLoad(options) {
        const value = wx.getStorageSync('vipuser');
        console.log(value)
        if (value) {
            this.setData({
                user: value
            })
        }
    }
});